package com.xtedu.examproject.controller;

import com.xtedu.examproject.entity.ExamQuestion;
import com.xtedu.examproject.entity.ExamQuestionRelation;
import com.xtedu.examproject.entity.TemplateDetail;
import com.xtedu.examproject.service.ExamQuestionRelaService;
import com.xtedu.examproject.service.TemplateDetailService;
import com.xtedu.examproject.util.CommonUtils;
import com.xtedu.examproject.util.UploadUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

/*
 * Author: xzy
 * Date: 2025/10/17
 * Last Modified by: xzy
 * Last Modified date: 2025/10/17
 * Description: TODO - Add file description here
 */
@WebServlet("/templateDetail")
public class TemplateDetailServlet extends HttpServlet {
    private TemplateDetailService templateDetailService;

    @Override
    public void init() throws ServletException {
        templateDetailService = new TemplateDetailService();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();

        // 处理 null 或空路径
        if (pathInfo == null || pathInfo.equals("/") || pathInfo.isEmpty()) {
            CommonUtils.sendErrorResponse(resp, "Invalid path");
            return;
        }

        // 标准化路径（去除前导和尾随斜杠）
        String path = pathInfo.replaceAll("^/|/$", "");

        switch (path) {
            case "findById" -> findById(req, resp);
            case "findByTemplateId" -> findByTemplateId(req, resp);
        }
    }

    private void findById(HttpServletRequest req, HttpServletResponse resp) {
        int id = Integer.parseInt(req.getParameter("id"));
        try {
            TemplateDetail templateDetail = templateDetailService.findById(id);
            if (templateDetail == null) {
                CommonUtils.sendErrorResponse(resp, "未找到该数据");
                return;
            }
            CommonUtils.sendSuccessResponse(resp, "查询成功", templateDetail);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void findByTemplateId(HttpServletRequest req, HttpServletResponse resp) {
        int templateId = Integer.parseInt(req.getParameter("templateId"));
        try {
            List<TemplateDetail> templateDetail = templateDetailService.findByTemplateId(templateId);
            if (templateDetail == null) {
                CommonUtils.sendErrorResponse(resp, "未找到该数据");
                return;
            }
            CommonUtils.sendSuccessResponse(resp, "查询成功", templateDetail);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();

        // 标准化路径（去除前导和尾随斜杠）
        String path = pathInfo.replaceAll("^/|/$", "");

        switch (path) {
            case "add" -> add(req, resp);
            case "update" -> update(req, resp);
            case "delete" -> delete(req, resp);
            case "upload" -> UploadUtil.uploadFile(req, resp);
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        try {
            TemplateDetail templateDetail = CommonUtils.getFormDataFromRequest(req, TemplateDetail.class);
            int result = templateDetailService.insert(templateDetail);
            if (result <= 0) {
                CommonUtils.sendErrorResponse(resp, "添加失败");
                return;
            }
            CommonUtils.sendSuccessResponse(resp, "添加成功", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        try {
            TemplateDetail templateDetail = CommonUtils.getFormDataFromRequest(req, TemplateDetail.class);
            int result = templateDetailService.update(templateDetail);
            if (result <= 0) {
                CommonUtils.sendErrorResponse(resp, "更新失败");
                return;
            }
            CommonUtils.sendSuccessResponse(resp, "更新成功", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        try {
            int id = Integer.parseInt(req.getParameter("id"));
            int result = templateDetailService.deleteById(id);
            if (result <= 0) {
                CommonUtils.sendErrorResponse(resp, "删除失败");
                return;
            }
            CommonUtils.sendSuccessResponse(resp, "删除成功", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}
